define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							ctrl.requireDataList1();
						};
					});
				},0)
			},
			initTime: function(){
		        var ctrl = this;
		        var sdate = new Date(new Date() - timeD_value - 60000-60000);
		        sdate.setSeconds(0);
		        sdate.setMilliseconds(0); 
		        ctrl.set("startDate",new Date(+sdate));

		        var edate = new Date(new Date() - timeD_value-60000);
		        edate.setSeconds(0);
		        edate.setMilliseconds(0); 
		        ctrl.set("endDate",new Date(+edate));
		    },

			needs:["serviceServerNodeserver"],

			node: Ember.computed.alias('controllers.serviceServerNodeserver.model'),

			gridColumns: Ember.A(),

			gridData: Ember.A(),

			pageIndex: 0,

			pageSize: 20,

			menuIdChange: Ember.observer("model.id", function(){
				var ctrl = this,
					menuId = this.get("model.id");

				this.set("pageIndex", 0);
				this.set("gridColumns", Ember.A());
				this.set('keyWord', '');
				this.requireColumns(menuId)
				.then(function(columns){
					ctrl.set("gridColumns", columns);
					return ctrl.requireDataList1();
				});
			}),

			requireColumns: function(menuId){
				var ctrl = this;
				var params = {
					menuId: menuId
				};
				if( isLocalDebugModel ){
					params = undefined;
				}
				return this.store.ajax("GET", "node/menus/columns", params)
				.then(function(response){

					var columns = Ember.A();
					var allowSort = false;
					Ember.A(response.result).forEach(function(item){
						var cloumn = {
							headerText: item.name,
							dataKey: item.dataKey,
							width: item.width,
							allowSort: allowSort,
							ensurePxWidth: !!item.width,
							_action: item.action,
							_fmtType: item.transform,
							cellFormatter: function(args, colume){
								if (args.row.type & $.wijmo.wijgrid.rowType.data) {
									var $container = args.$container,
										val = args.row.data[colume.dataKey];
									if(colume._action){
										$container.addClass("btn-link");
									}
									if(colume._fmtType){
										$container.html( APM.util.indicatorFormatter(colume._fmtType)(val) );
										return true;
									}
									if(String(val).length > 60){
										var $item = $('<div class="column-summary"></div>').appendTo($container);
										$item.html(APM.util.escape(val));
										$container.attr("title",val);
										return true;
									}
								}
							}
						};
						allowSort = true;
						columns.push(cloumn);
					});
					return columns;
				});
			},

			requireDataList: function(args){
				var ctrl = this;

				var params = {
					appId: "default",
					nodeId: this.get("node.id")
				};
				Ember.merge(params, args);
				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}
				return this.store.ajax("GET", "node/menus/data", params);
			},

			requireDataList1: function(){
				var ctrl = this;

				var params = {
					menuId: this.get("model.id"),
					keyWord: this.get("keyWord"),
					startDate: +this.get("startDate"),
					endDate: +this.get("endDate"),
					pageIndex: this.get("pageIndex"),
					orderBy: this.get("orderBy"),
					desc: this.get("desc")
				};

				//开发调试模式参数
				if( isLocalDebugModel ){
					params = {};
				}

				this.requireDataList(params)
				.then(function(response){
					ctrl.set("gridData", Ember.A(response.result));
					ctrl.set("totalRows", typeof response.meta.total=="undefined"?-1:response.meta.total);
					if (response.result.length == 0) {
						ctrl.set("pageIndex",0);
						ctrl.set("pageSize",20);
						ctrl.set("totalRows",0);
					}
				});
			},

			actions: {
				gridCellClicked: function(e, args, view){
					var ctrl = this,
						row = args.cell.row(),
						column = args.cell.column();
					if (row && row.type & $.wijmo.wijgrid.rowType.data) {
						if(column._action && column._action.name == "showDetail"){
							this.send(column._action.name, row.data, column.dataKey, column._action.args);
							Broada.getView("businessDetailNodedetailDynamicDialog").openDialog();
						}
					}
				},

				pageIndexChanged: function(e, data, view){
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);
					this.requireDataList1();
				},

				gridSorted: function(e, data, view){
					view.ensureControl(false);
					this.set("orderBy", data.column.dataKey);
					this.set("desc", data.sortDirection=="descending"||undefined);
					this.requireDataList1();
				},

				gridSorted2: function(e, data, view){
					view.ensureControl(false);
					this.set("orderBy", data.column.dataKey);
					this.set("desc", data.sortDirection=="descending"||undefined);
					this.requireDataList2();
				},

				search: function() {
					this.set("pageIndex", 0);
					this.requireDataList1();
				},

				timeRangesChange: function(timeRange){
					this.set("startDate", timeRange.startDate);
					this.set("endDate", timeRange.endDate);

					if(this.get("gridColumns.length") > 0){
						this.set("pageIndex", 0);
						this.requireDataList1();
					}
				}
			}
		});

		Controller.reopen({
			gridColumns2: Ember.A(),

			gridData2: Ember.A(),

			pageIndex2: 0,

			pageSize2: 20,

			requireDataList2: function(){
				var ctrl = this,
					args = this.get("dataList2Args");

				var params = {
					startDate: +this.get("startDate"),
					endDate: +this.get("endDate"),
					pageIndex: this.get("pageIndex2"),
					orderBy: this.get("orderBy"),
					desc: this.get("desc")
				};
				Ember.A(args).forEach(function(arg){
					params[arg.key] = arg.value;
				});
				delete params.next_menu_id;

				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}
				
				this.requireDataList(params)
				.then(function(response){
					ctrl.set("gridData2", Ember.A(response.result));
					ctrl.set("totalRows2", response.meta.total);
				});
			},

			actions: {
				pageIndexChanged2: function(e, data, view){
					this.set("pageIndex2", data.newPageIndex);

					this.requireDataList2();
				},

				showDetail: function(data, dataKey, args){
					var ctrl = this,
						menuId = getValueFromArgs(args, "next_menu_id");

					this.set("dataList2Args", [
						{key: "menuId", value: menuId},
						{key: "dataId", value: data.id},
						{key: "dataKey", value: dataKey}
					].concat(args));
					this.set("pageIndex2", 0);
					this.set("gridColumns2", Ember.A());

					this.requireColumns(menuId)
					.then(function(columns){
						ctrl.set("gridColumns2", columns);
						ctrl.requireDataList2();
					});
				}
			}
		});

		function getValueFromArgs(args, key){
			var arg = Ember.A(args).findBy("key", key);
			return arg&&arg.value;
		}

		APM.ServiceServerNodeserverDynamicController = Controller;

		return Controller;
	}
);	